<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<div class="portlet box green">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-gift"></i>护士
        </div>
    </div>
    <div class="portlet-body form">
        <form id="nurseForm" class="form-horizontal form-bordered" action="/nurse/save" method="post">
            <div class="form-group">
                <label class="col-md-3 control-label">用户名:</label>
                <div class="col-md-4">
                    <input type="text" name="userName" class="form-control" value="${nurse.userName}" maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">真实姓名:</label>
                <div class="col-md-4">
                    <input type="text" name="name" class="form-control" value="${nurse.name}" maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">头像:</label>
                <div class="col-md-9">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" id="checkAvatarUrl"
                             style="width: 200px; height: 150px;">
                            <c:if test="${not empty nurse.avatarUrl}">
                                <img src="<c:url value="/common/showImage?filePath=${nurse.avatarUrl}"></c:url>"
                                                  alt=""/>
                            </c:if>
                        </div>
                        <div>
							<span class="btn default btn-file">
								<span class="fileinput-new">选择图片</span>
								<span class="fileinput-exists">编辑</span>
								<input type="file" name="avatarUrl" value="${nurse.avatarUrl}">
							</span>
                            <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput">删除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">密码:</label>
                <div class="col-md-4">
                    <input type="text" name="password" class="form-control" value="${nurse.password}" maxlength="32"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">所在科室:</label>
                <div class="col-md-4">
                    <div class="input-icon right">
                        <i class="fa"></i>
                        <input type="hidden" id="departmentId" name="departmentId" value="${nurse.departmentId}"/>
                        <input type="text" class="form-control" id="departmentName" name="departmentName"
                               onclick="showDepartment()" readonly
                               value="${nurse.departmentName}" maxlength="200"/>
                    </div>
                </div>
                <div class="col-md-2">
                    <button type="button" class="btn btn-default" onclick="clearParentDepartment()">清空</button>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">年龄:</label>
                <div class="col-md-4">
                    <input type="text" name="age" class="form-control" value="${nurse.age}" maxlength="3"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">性别:</label>
                <div class="col-md-4">
                    <div class="radio-list">
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="1" <c:if test="${nurse.sex == 1}">checked</c:if>> 男 </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="0" <c:if test="${nurse.sex == 0}">checked</c:if>> 女 </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="2" <c:if test="${nurse.sex != 0 && nurse.sex != 1}">checked</c:if>> 保密 </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">手机号:</label>
                <div class="col-md-4">
                    <input type="text" name="phone" class="form-control" value="${nurse.phone}" maxlength="20"/>
                </div>
            </div>
            <div class="form-actions">
                <div class="col-md-9 col-md-offset-3">
                    <button type="button" class="btn btn-primary" onclick="saveNurse(this)">
                        <spring:message code="btn.save"></spring:message>
                    </button>
                    <button type="reset" class="btn btn-default" onclick="cancel()">
                        <spring:message code="btn.return"></spring:message>
                    </button>
                </div>
            </div>
            <input type="hidden" name="checkAvatarUrl">
            <input type="hidden" name="id" value="${nurse.id}">

        </form>
    </div>
</div>
<div class="modal fade" id="departmentModal" tabindex="-1" role="basic" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">选择上级菜单</h4>
            </div>
            <div class="modal-body">
                <ul id="departmentTree" class="ztree" style="margin-top:0; width:160px;"></ul>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn blue" onclick="saveParentDepartment()">确定</button>
                <button type="button" class="btn default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="<c:url value="/static/custom/datepicker-util.js"></c:url>"></script>
<script type="text/javascript">
    $(function () {
        $('#nurseForm').validate();
    });

    var setting = {
        view: {
            showIcon: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    $(function () {
        $('#departmentForm').validate({
            rules: {
                sort: {
                    number: true
                }
            }
        });
    });

    // 保存上级菜单
    function saveParentDepartment() {
        var nodes = $.fn.zTree.getZTreeObj("departmentTree").getSelectedNodes();
        if (nodes[0].id != $('#id').val()) {
            $('#departmentId').val(nodes[0].id);
            $('#departmentName').val(nodes[0].name);
            // 隐藏弹窗
            $('#departmentModal').modal('hide');
        }
        else {
            alert('不能选择当前菜单');
        }
    }

    // 清空上级菜单
    function clearParentDepartment() {
        $('#departmentId').val('');
        $('#departmentName').val('');
    }

    // 显示弹窗
    function showDepartment() {
        // 加载菜单的树状结构
        $.ajax({
            type: 'post',
            url: '<c:url value="/hospitalDepartment/getDepartmentForTree"></c:url>',
            data: {currentId: $('#id').val()},
            dataType: 'json',
            success: function (data) {
                $.fn.zTree.init($("#departmentTree"), setting, data);
            }
        });
        $('#departmentModal').modal('show');
    }

    /**
     * 护士信息新建或修改
     * @param obj
     */
    function saveNurse(obj) {
        if ($('#nurseForm').valid()) {


            var checkAvatarUrl = $("#checkAvatarUrl").children("img").attr("src");
            if (checkAvatarUrl != undefined && $.trim(checkAvatarUrl).length > 0){
                $("input[name='checkAvatarUrl']").val(checkAvatarUrl);
            }
            var formData = new FormData($('#nurseForm')[0]);
            $.ajax({
                type: 'post',
                url: '<c:url value="/nurse/save"></c:url>',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (data) {
                    alert(messages[data.result]);
                    search();
                    queryStatistics();
                    $(".panel-search").show();
                }
            });
        }
    }
</script>